

import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { ResultState } from "../../SDKDefine";
@registerClass('KSRewardAd')
export default class KSRewardAd extends BaseAd {


    hide(): void {
    }
    load(): void {
        if (this.ad) {
            this.ad.load();
        }
    }
    onClose(res: any) {
        console.log('NativeTest onClose ', res)
        if (res && res.isEnded || res === undefined) {
            this.callResultCallback(ResultState.YES)
        } else {
            this.callResultCallback(ResultState.NO)
        }
    }

    create(): void {

        // let sdkVersion = this.sdk.getSystemInfoSync().SDKVersion;
        if (!this.ad) {
            console.log(' 不支持多例')
            this.ad = this.sdk.createRewardedVideoAd({ adUnitId: this.adUnitID })
            // this.rewardAd.onLoad(this.onLoad.bind(this))
            this.ad.onError(this.getFunc(this.onError))
            this.ad.onClose(this.getFunc(this.onClose))
        } else {
            //微信会在第一次创建的时候默认load一次。
            // this.rewardAd.load();
        }

    }

    destroy(): void {
        if (this.ad) {
            // this.rewardAd.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.offClose(this.getFunc(this.onClose))
            this.ad.destroy();
            this.ad = null;
        }
    }
    show() {
        if (this.ad) {
            let p = this.ad.show()
            p.then(function (result) {
                // 激励视频展示成功
                console.log(`show rewarded video ad success, result is ${result}`)
                this.pause()
            }).catch(function (error) {
                // 激励视频展示失败
                console.log(`show rewarded video ad failed, error is ${error}`)
            })
        }

    }



}
